###################################################################################################
# Franchise Expansion and Legislative Representation in the Early United States ###################
# Rivers and Harbors Analysis #####################################################################
# Stephen Ansolabehere, Jaclyn Kaslovsky, and Michael Olson #######################################
###################################################################################################

###################################################################################################
# description #####################################################################################
###################################################################################################

# this script produces all results using rivers and harbors data. 

###################################################################################################
# working directory, packages, and function #######################################################
###################################################################################################

  require("lfe")
  require("dplyr")
  require("ggplot2")
  require("stargazer")
  require("usmap")
  require("starpolishr")

  ditch_the_axes <- theme(
    axis.text = element_blank(),
    axis.line = element_blank(),
    axis.ticks = element_blank(),
    panel.border = element_blank(),
    panel.grid = element_blank(),
    axis.title = element_blank()
  )

  setwd("C:/Users/flyfi/Dropbox/Research/franchise_expansion/franchise_expansion_R&R/Replication Package")

###################################################################################################
# load data, create summary statistics table ######################################################
###################################################################################################

# data 

  dat <- read.csv("rh_data_foranalysis.csv")

# descriptive stats

  tabdat <- dat[,c("log_div","log_trunc","prop_repeal","tax_repeal"
                   ,"pct_slave","pct_urb","pct_urb25","logpop","delegation_size"
                   ,"white","citizen")]
  
  pbp_summary_sg <- stargazer(tabdat,
                              covariate.labels=c("ln(All R\\&H Appropriations)","ln(Targeted R\\&H Appropriations)","No Property Requirement","No Tax Requirement","Percent Enslaved",
                                                 "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                                 "White Requirement","Citizen Requirement"),
                              summary.stat = c("mean","median","sd","min","max","n"),digits=2,
                              title="Rivers and Harbors Appropriations Summary Statistics",
                              label="pbp_summary")
  
  cat(pbp_summary_sg, sep = '\n', file = "pbp_summary.tex") 
  
# make line plot over time of spending -- all spending measure
  
  df_sum <- dat %>%
    group_by(year) %>%
    summarise(total_spending = sum(div_total_spending,na.rm = T))
  
  df_sum$total_spending <- df_sum$total_spending/10000
  
  ggplot(data=df_sum) +
    geom_line(aes(x=year, y=total_spending)) +
    xlab("Year") + ylab("Total Appropriations ($10,000s)") + 
    theme_bw()  +
    theme(text = element_text(size=18))

  ggsave("line.jpeg",width = 10, height = 8, units="in", dpi=800) 
  
# make line plot over time of spending -- targeted spending measure
  
  df_sum_trunc <- dat %>%
    group_by(year) %>%
    summarise(trunc_total_spending = sum(trunc_total_spending,na.rm = T))
  
  df_sum_trunc$trunc_total_spending <- df_sum_trunc$trunc_total_spending/10000
  
ggplot(data=df_sum_trunc) +
    geom_line(aes(x=year, y=trunc_total_spending)) +
    xlab("Year") + ylab("Total Targeted Appropriations ($10,000s)") + 
    theme_bw()  +
    theme(text = element_text(size=18))
  
  ggsave("line_trunc.jpeg",width = 10, height = 8, units="in",dpi=800) 
  
# make a map of spending -- all spending measure
  
  rhdat <- dat %>%
    group_by(state) %>%
    summarise(total_spending = sum(div_total_spending,na.rm = T))

  df <- us_map(regions = "states")
  colnames(rhdat)[colnames(rhdat)=="state"] <- "abbr"

  tomap_final <- left_join(df, rhdat, by.x = "abbr")

  all <- tomap_final[,c("fips","total_spending")]
  all$total_spending <- all$total_spending/10000
  #replace west virginia to be same as virginia
  all$total_spending[all$fips==54] <-  all$total_spending[all$fips==51][1]

  map_traveled <-plot_usmap(data=all, values="total_spending", color="grey60")  +
    theme_bw() + ditch_the_axes +
    labs(fill = "Total Appropriations ($10,000s):  ") +
    scale_fill_gradient(low="grey88", high="dodgerblue4", na.value="white") +
    theme(legend.position="bottom") +
    theme(legend.title=element_text(size=12), legend.text=element_text(size=10))+
    theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

  ggsave("map_water.jpeg",width = 10, height = 8, units="in",dpi=800)
  
# make a map of spending -- targeted spending measure
  
  rhdat_trunc <- dat %>%
    group_by(state) %>%
    summarise(total_spending_trunc = sum(trunc_total_spending,na.rm = T))
  
  colnames(rhdat_trunc)[colnames(rhdat_trunc)=="state"] <- "abbr"
  
  df <- us_map(regions = "states")
  tomap_final_trunc <- left_join(df, rhdat_trunc, by.x = "abbr")

  all_trunc <- tomap_final_trunc[,c("fips","total_spending_trunc")]
  all_trunc$total_spending_trunc <- all_trunc$total_spending_trunc/10000
  #replace west virginia to be same as virginia
  all_trunc$total_spending_trunc[all_trunc$fips==54] <-  all_trunc$total_spending_trunc[all_trunc$fips==51][1]
  
  map_traveled_trunc <-plot_usmap(data=all_trunc, values="total_spending_trunc", color="grey60")  +
    theme_bw() + ditch_the_axes +
    labs(fill = "Total Targeted Appropriations ($10,000s):  ") +
    scale_fill_gradient(low="grey88", high="dodgerblue4", na.value="white") +
    theme(legend.position="bottom") +
    theme(legend.title=element_text(size=12), legend.text=element_text(size=10))+
    theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

  ggsave("map_water_trunc.jpeg",width = 10, height = 8, units="in",dpi=800)
  
# descriptive statistics
  
  # most places have no spending, even in years when at least some spending is passed
  
  median(dat$div_total_spending[dat$prop_repeal==0],na.rm=T)

  median(dat$div_total_spending[dat$prop_repeal==0 & dat$indata==1],na.rm=T)
  
  # what is the mean spending of prop_repeal?
  
  fileConn<-file("mean_control.tex")
  writeLines(format(round(mean(dat$div_total_spending[dat$prop_repeal==0],na.rm=T),0),format="d",big.mark=','), fileConn)
  close(fileConn)
  
# plot of results -- all spending measure
  
  # find first year each state relaxes property-holding requirements
  
  min_years <- aggregate(year~state,data=dat[dat$prop_repeal==1,],FUN=min)
  min_years <- plyr::rename(min_years,replace=c("year"="min_year"))
  
  dat <- merge(dat,min_years,by="state",all.x=T)

  dat$runner <- dat$year-dat$min_year+1
  dat_plot <- dat[!is.na(dat$runner),]
  
    ggplot(dat_plot,aes(x=runner,y=log_div))+
      geom_smooth(data=dat_plot[dat_plot$runner<=0 & dat_plot$runner>=-5,],colour="black",size=2)+
      geom_smooth(data=dat_plot[dat_plot$runner>0 & dat_plot$runner<=5,],colour="black",size=2)+
      geom_vline(xintercept=0.5,linetype=2)+
      xlab("Years to Adoption")+
      ylab("ln(All Rivers & Harbors Appropriations)")+
      theme_minimal()

  ggsave("prop_repeal_rh_plot.jpeg",width = 10, height = 4, units="in",dpi=800)
  
# plot of results -- targeted spending measure

    ggplot(dat_plot,aes(x=runner,y=log_trunc))+
      geom_smooth(data=dat_plot[dat_plot$runner<=0 & dat_plot$runner>=-5,],colour="black",size=2)+
      geom_smooth(data=dat_plot[dat_plot$runner>0 & dat_plot$runner<=5,],colour="black",size=2)+
      geom_vline(xintercept=0.5,linetype=2)+
      xlab("Years to Adoption")+
      ylab("ln(Targeted Rivers & Harbors Appropriations)")+
      theme_minimal()
  
  ggsave("prop_repeal_rh_plot_trunc.jpeg",width = 10, height = 4, units="in",dpi=800)
  
###################################################################################################
# regression results ##############################################################################
###################################################################################################  
  
# Fixed effects

  fe_nocov <- felm(log_div~prop_repeal
                 |state+year|0|state,data=dat)

  fe_cov1 <- felm(log_div~prop_repeal
                +pct_slave+pct_urb+pct_urb25+logpop
                +delegation_size
                |state+year|0|state,data=dat)
  
  fe_cov2 <- felm(log_div~prop_repeal
                +pct_slave+pct_urb+pct_urb25+logpop
                +delegation_size
                +tax_repeal+white+citizen
                |state+year|0|state,data=dat)

# Linear State Trends

  li_nocov <- felm(log_div~prop_repeal
                   |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  li_cov1 <- felm(log_div~prop_repeal
                  +pct_slave
                  +pct_urb
                  +pct_urb25
                  +logpop
                  +delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)

  li_cov2 <- felm(log_div~prop_repeal
                +pct_slave+pct_urb+pct_urb25+logpop
                +delegation_size
                +tax_repeal+white+citizen
                |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)

# quadratic State Trends

  qu_nocov <- felm(log_div~prop_repeal
                   |(as.numeric(index)+as.numeric(index2)):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  qu_cov1 <- felm(log_div~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop
                  +delegation_size
                  |(as.numeric(index)+as.numeric(index2)):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  qu_cov2 <- felm(log_div~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop
                  +delegation_size
                  +tax_repeal+white+citizen
                  |(as.numeric(index)+as.numeric(index2)):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
# Fixed effects, single-state projects
  
  fe_trunc_nocov <- felm(log_trunc~prop_repeal
                   |state+year|0|state,data=dat)
  
  fe_trunc_cov1 <- felm(log_trunc~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop
                  +delegation_size
                  |state+year|0|state,data=dat)
  
  fe_trunc_cov2 <- felm(log_trunc~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop
                  +delegation_size
                  +tax_repeal+white+citizen
                  |state+year|0|state,data=dat)
  
# Linear State Trends, single-state projects
  
  li_trunc_nocov <- felm(log_trunc~prop_repeal
                   |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  li_trunc_cov1 <- felm(log_trunc~prop_repeal
                  +pct_slave
                  +pct_urb
                  +pct_urb25
                  +logpop
                  +delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  li_trunc_cov2 <- felm(log_trunc~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop
                  +delegation_size
                  +tax_repeal+white+citizen
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
# quadratic State Trends, single-state projects
  
  qu_trunc_nocov <- felm(log_trunc~prop_repeal
                   |(as.numeric(index)+as.numeric(index2)):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  qu_trunc_cov1 <- felm(log_trunc~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop
                  +delegation_size
                  |(as.numeric(index)+as.numeric(index2)):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  qu_trunc_cov2 <- felm(log_trunc~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop
                  +delegation_size
                  +tax_repeal+white+citizen
                  |(as.numeric(index)+as.numeric(index2)):factor(state)+factor(state)+factor(year)|0|state,data=dat)

# report estimates

  pbp_li_sg <- stargazer(fe_nocov,fe_cov1,fe_cov2,qu_nocov,qu_cov1,qu_cov2,
                         add.lines = list(c("State Trend","Fixed","Fixed","Fixed","Quadratic","Quadratic","Quadratic")),
                         omit.stat = c("rsq", "f", "ser","adj.rsq"),font.size = "footnotesize",
                         no.space = TRUE,table.placement = "H",
                         dep.var.labels = c("ln(Rivers and Harbors Appropriations+1)"),
                         covariate.labels=c("No Property Requirement","Percent Enslaved",
                                            "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                            "No Tax Requirement","White Requirement","Citizen Requirement"),
                         label="rh_panel",notes.append = FALSE,notes.label = "",column.sep.width = "5pt",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                         notes=paste("\\parbox[t]{0.9\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(fe_nocov$clustervar[[1]])),"states. 
                                     The unit of observation is the state-year. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         omit.table.layout="#",
                         title="Rivers and Harbors Appropriations, Alternative State Trends")
  
  pbp_li_sg_trunc <- stargazer(fe_trunc_nocov,fe_trunc_cov1,fe_trunc_cov2,qu_trunc_nocov,qu_trunc_cov1,qu_trunc_cov2,
                         add.lines = list(c("State Trend","Fixed","Fixed","Fixed","Quadratic","Quadratic","Quadratic")),
                         omit.stat = c("rsq", "f", "ser","adj.rsq"),font.size = "footnotesize",
                         no.space = TRUE,table.placement = "H",
                         dep.var.labels = c("ln(Rivers and Harbors Appropriations+1)"),
                         covariate.labels=c("No Property Requirement","Percent Enslaved",
                                            "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                            "No Tax Requirement","White Requirement","Citizen Requirement"),
                         label="rh_panel",notes.append = FALSE,notes.label = "",column.sep.width = "5pt",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                         notes=paste("\\parbox[t]{0.9\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(fe_trunc_nocov$clustervar[[1]])),"states. 
                                     The unit of observation is the state-year. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         omit.table.layout="#",
                         title="Rivers and Harbors Appropriations, Alternative State Trends")
  
  star.panel.out <- star_panel(pbp_li_sg,pbp_li_sg_trunc,panel.label.fontface="bold",
                               panel.names = c("All Appropriations", "Targeted Appropriations"), same.summary.stats = T)
  cat(star.panel.out, sep = '\n', file = "pbp_li.tex")
  
  
  pbp_fe_sg <- stargazer(li_nocov,li_cov1,li_cov2,li_trunc_nocov,li_trunc_cov1,li_trunc_cov2,
                           omit.stat = c("rsq", "f", "ser"),
                           float.env="table",no.space=TRUE,table.placement = "!ht",
                          dep.var.caption = c("ln(Rivers and Harbors Appropriations+1)"),
                          dep.var.labels = c("All Appropriations","Targeted Appropriations"),
                          covariate.labels=c("No Property Requirement","Percent Enslaved",
                                             "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                             "No Tax Requirement","White Requirement","Citizen Requirement"),
                          label="rh_fe",notes.append = FALSE,notes.label = "",
                          report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                         notes=paste("\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(li_cov1$clustervar[[1]])),"states. 
                                     The unit of observation is the state-year. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                          table.layout ="-ld-t-as-n",
                          title="Franchise Expansion and Rivers and Harbors Appropriations")
  
  cat(pbp_fe_sg, sep = '\n', file = "pbp_fe.tex")

# parallel trends test

  dwh_lag1   <- felm(log_div~prop_repeal
                   +(pr1+pr_1)
                   +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                   |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                   ,data=dat)
  
  dwh_lag2   <- felm(log_div~prop_repeal
                   +(pr1+pr2+pr_1+pr_2)
                   +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                   |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                   ,data=dat)

  dwh_lag3   <- felm(log_div~prop_repeal
                   +(pr1+pr2+pr3+pr_1+pr_2+pr_3)
                   +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                   |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                   ,data=dat)
  
  dwh_trunc_lag1   <- felm(log_trunc~prop_repeal
                     +(pr1+pr_1)
                     +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                     |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                     ,data=dat)
  
  dwh_trunc_lag2   <- felm(log_trunc~prop_repeal
                     +(pr1+pr2+pr_1+pr_2)
                     +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                     |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                     ,data=dat)
  
  dwh_trunc_lag3   <- felm(log_trunc~prop_repeal
                     +(pr1+pr2+pr3+pr_1+pr_2+pr_3)
                     +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                     |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                     ,data=dat)

  laglead_pbp_sg <- stargazer(dwh_lag1,dwh_lag2,dwh_lag3,
                              notes.append = FALSE,notes.label = "",
                              report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                              omit.stat = c("rsq", "ser","f"),float.env = "table",
                              dep.var.labels = "ln(Rivers and Harbors Spending+1)",single.row = TRUE,
                              covariate.labels=c("No Property Requirement",
                                                 "No Prop. Req. $+1$","No Prop. Req. $+2$","No Prop. Req. $+3$",
                                                 "No Prop. Req. $-1$","No Prop. Req. $-2$","No Prop. Req. $-3$",
                                                 "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size"
                                              ),
                              notes=paste("\\parbox[t]{0.85\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(dwh_lag1$clustervar[[1]])),"states. 
                                     The unit of observation is the state-year. Minus signs indicate lagged variables, while plus signs indicate led variables. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                              label="pt_pb",
                             omit.table.layout="#",
                                                           title="Parallel Trends Test, Rivers and Harbors Appropriations") 
  
  laglead_trunc_pbp_sg <- stargazer(dwh_trunc_lag1,dwh_trunc_lag2,dwh_trunc_lag3,
                              notes.append = FALSE,notes.label = "",
                              report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                              omit.stat = c("rsq", "ser","f"),float.env = "table",
                              dep.var.labels = "ln(Rivers and Harbors Appropriations+1)",single.row = TRUE,
                              covariate.labels=c("No Property Requirement",
                                                 "No Prop. Req. $+1$","No Prop. Req. $+2$","No Prop. Req. $+3$",
                                                 "No Prop. Req. $-1$","No Prop. Req. $-2$","No Prop. Req. $-3$",
                                                 "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size"),
                              notes="\\parbox[t]{0.85\\textwidth}{\\footnotesize \\textit{Note}: Clustered standard errors
                              in parentheses. Estimates are from OLS regressions with state linear trends and year fixed effects.
                              The unit of observation is the state-Congress, with data aggregated from the legislator level. Minus signs indicate lagged variables, while plus signs indicate led variables. $^{*}p<0.10$, $^{**}p<0.05$.}",
                              label="pt_pb",
                             omit.table.layout="#",
                             
                              title="Parallel Trends Test, Rivers and Harbors Appropriations") 
  
  star.panel.out <- star_panel(laglead_pbp_sg,laglead_trunc_pbp_sg,panel.label.fontface="bold",
                               panel.names = c("All Appropriations", "Targeted Appropriations"), same.summary.stats = T)
  cat(star.panel.out, sep = '\n', file = "laglead_pbp.tex")
  
# state-dropper  

  states <- sort(unique(dat$state))
  state_prop <- rep(NA,length(states))
  state_prop_se <- state_prop

  for(i in 1:length(states)){
    
    loop <- dat[dat$state!=states[i],]
    
    reg   <- felm(log_div~prop_repeal
                +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                ,data=loop)
  
    state_prop[i] <- coef(reg)["prop_repeal"]
    state_prop_se[i] <- sqrt(reg$clustervcv["prop_repeal","prop_repeal"])

  }
  
  plotdat <- as.data.frame(cbind(state_prop,state_prop_se))
  plotdat <- cbind(states,plotdat)
  plotdat$Appropriations <- "All"
  
  loop <- NULL
  reg  <- NULL
  
  state_prop_trunc <- rep(NA,length(states))
  state_prop_se_trunc <- state_prop_trunc
  
  for(i in 1:length(states)){
    
    loop <- dat[dat$state!=states[i],]
    
    reg   <- felm(log_trunc~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                  ,data=loop)
    
    state_prop_trunc[i] <- coef(reg)["prop_repeal"]
    state_prop_se_trunc[i] <- sqrt(reg$clustervcv["prop_repeal","prop_repeal"])
    
  }
  
  plotdat_trunc <- as.data.frame(cbind(state_prop_trunc,state_prop_se_trunc))
  plotdat_trunc <- cbind(states,plotdat_trunc)
  plotdat_trunc$Appropriations <- "Targeted"
  colnames(plotdat_trunc) <- colnames(plotdat)
  
  plotdat <- rbind(plotdat,plotdat_trunc)
  
  ggplot(data=plotdat,aes(x=states,y=state_prop,group=Appropriations,colour=Appropriations))+
    geom_point(position=position_dodge(0.75))+
    geom_errorbar(aes(ymin=state_prop-2*state_prop_se,ymax=state_prop+2*state_prop_se),
                  width=0.5,position=position_dodge(0.75))+
    geom_errorbar(aes(ymin=state_prop-1.65*state_prop_se,ymax=state_prop+1.65*state_prop_se),
                  width=0.3,position=position_dodge(0.75))+
    geom_hline(yintercept = 0,colour="indianred",linetype=2,size=1)+
    xlab("Dropped State")+
    ylab("Coefficient on 'Property Requirement Repeal'")+
    scale_colour_grey(start=0.1,end=0.6)+
    theme_minimal()
  
  ggsave("state_dropper_pbp.jpeg",width = 8, height = 4, units="in",dpi=800)
  
# year-dropper

  years <- sort(unique(dat$year))
  years_prop <- rep(NA,length(years))
  years_prop_se <- years_prop

  for(i in 1:length(years)){
    
    loop2 <- dat[dat$year!=years[i],]
    
    reg   <- felm(log_div~prop_repeal
                +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                ,data=loop2)
    
    years_prop[i] <- coef(reg)["prop_repeal"]
    years_prop_se[i] <- sqrt(reg$clustervcv["prop_repeal","prop_repeal"])

  }
  
  plotdat_year <- as.data.frame(cbind(years_prop,years_prop_se))
  plotdat_year <- cbind(years,plotdat_year)
  plotdat_year$Appropriations <- "All"
  
  years_prop_trunc <- rep(NA,length(years))
  years_prop_se_trunc <- years_prop_trunc
  loop2 <- NULL
  reg <- NULL
  
  for(i in 1:length(years)){
    
    loop2 <- dat[dat$year!=years[i],]
    
    reg   <- felm(log_trunc~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                  ,data=loop2)
    
    years_prop_trunc[i] <- coef(reg)["prop_repeal"]
    years_prop_se_trunc[i] <- sqrt(reg$clustervcv["prop_repeal","prop_repeal"])
    
  }
  
  plotdat_year_trunc <- as.data.frame(cbind(years_prop_trunc,years_prop_se_trunc))
  plotdat_year_trunc <- cbind(years,plotdat_year_trunc)
  plotdat_year_trunc$Appropriations <- "Targeted"
  colnames(plotdat_year_trunc) <- colnames(plotdat_year)
  
  plotdat_year <- rbind(plotdat_year,plotdat_year_trunc)
  
  plotdat_year$years <- as.integer(as.character(plotdat_year$years)) 
  
  ggplot(data=plotdat_year,aes(x=years,y=years_prop,group=Appropriations,colour=Appropriations))+
    geom_point(position=position_dodge(0.75))+
    geom_errorbar(aes(ymin=years_prop-2*years_prop_se,ymax=years_prop+2*years_prop_se),
                  width=0.5,position=position_dodge(0.75))+
    geom_errorbar(aes(ymin=years_prop-1.65*years_prop_se,ymax=years_prop+1.65*years_prop_se),
                  width=0.3,position=position_dodge(0.75))+
    geom_hline(yintercept = 0,colour="indianred",linetype=2,size=1)+
    xlab("Dropped Year")+
    ylab("Coefficient on 'Property Requirement Repeal'")+
    scale_colour_grey(start=0.1,end=0.6)+
    theme_minimal()

  ggsave("year_dropper_pbp.jpeg",width = 8, height = 4, units="in",dpi=800)
  

# alternative spending measures
  
  fe_anymoney_nocov <- felm(any_money~prop_repeal
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  fe_anymoney_cov1 <- felm(any_money~prop_repeal
                           +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                           |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  fe_anymoney_cov2 <- felm(any_money~prop_repeal
                           +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                           +tax_repeal+white+citizen
                           |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)

  fe_pc_nocov <- felm(logpercap~prop_repeal
                      |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  fe_pc_cov1 <- felm(logpercap~prop_repeal
                     +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                     |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  fe_pc_cov2 <- felm(logpercap~prop_repeal
                     +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                     +tax_repeal+white+citizen
                     |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  pbp_alt_sg <- stargazer(fe_anymoney_nocov,fe_anymoney_cov1,fe_anymoney_cov2,fe_pc_nocov,fe_pc_cov1,fe_pc_cov2,
                          omit.stat = c("rsq", "f", "ser","adj.rsq"),font.size = "footnotesize",
                          no.space = TRUE,column.sep.width = "0pt",
                          dep.var.labels = c("Any Money Received (0 - 1)","ln(Per Capita R \\& H Appropriations + 0.01)"),
                          covariate.labels=c("No Property Requirement","Percent Enslaved",
                                             "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                             "No Tax Requirement","White Requirement","Citizen Requirement"),
                          label="rh_panel_altoutcomes",notes.append = FALSE,notes.label = "",
                          report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                          notes=paste("\\parbox[t]{0.95\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(fe_anymoney_nocov$clustervar[[1]])),"states. 
                                     The unit of observation is the state-year. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         omit.table.layout="#",
                          title="Rivers and Harbors Appropriations, Alternative Outcome Measures")
  
  fe_anymoney_nocov_trunc <- felm(any_money_trunc~prop_repeal
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  fe_anymoney_cov1_trunc <- felm(any_money_trunc~prop_repeal
                           +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                           |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  fe_anymoney_cov2_trunc <- felm(any_money_trunc~prop_repeal
                           +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                           +tax_repeal+white+citizen
                           |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  fe_pc_nocov_trunc <- felm(logpercap_trunc~prop_repeal
                      |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  fe_pc_cov1_trunc <- felm(logpercap_trunc~prop_repeal
                     +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                     |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  fe_pc_cov2_trunc <- felm(logpercap_trunc~prop_repeal
                     +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                     +tax_repeal+white+citizen
                     |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)
  
  pbp_alt_sg_trunc <- stargazer(fe_anymoney_nocov_trunc,fe_anymoney_cov1_trunc,fe_anymoney_cov2_trunc,
                          fe_pc_nocov_trunc,fe_pc_cov1_trunc,fe_pc_cov2_trunc,
                          omit.stat = c("rsq", "f", "ser","adj.rsq"),font.size = "footnotesize",
                          no.space = TRUE,column.sep.width = "0pt",
                          dep.var.labels = c("Any Money Received (0 - 1)","ln(Per Capita R \\& H Appropriations + 0.01)"),
                          covariate.labels=c("No Property Requirement","Percent Enslaved",
                                             "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                             "No Tax Requirement","White Requirement","Citizen Requirement"),
                          label="rh_panel_altoutcomes",notes.append = FALSE,notes.label = "",
                          report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                          notes=paste("\\parbox[t]{0.8\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(li_cov1$clustervar[[1]])),"states. 
                                     The unit of observation is the state-year. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                          omit.table.layout="#",
                          title="Rivers and Harbors Appropriations, Alternative Outcome Measures")
  
  pbp_alt_sg <- star_panel(pbp_alt_sg,pbp_alt_sg_trunc,panel.label.fontface="bold",
                               panel.names = c("All Appropriations", "Targeted Appropriations"), same.summary.stats = T)
  cat(pbp_alt_sg, sep = '\n', file = "pbp_alt.tex")
  
  
# alternative samples
  
  colonies <- c("MA","NH","CT","NY","PA","VA","NJ","DE","MD","GA","SC","NC","RI")
  
  dat$colony <- ifelse(is.element(dat$state,colonies),1,0)
  
  fe_orig13_nocov <- felm(log_div~prop_repeal
                       |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$colony==1,])
  
  fe_orig13_cov1 <- felm(log_div~prop_repeal
                      +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                      |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$colony==1,])
  
  fe_orig13_cov2 <- felm(log_div~prop_repeal
                      +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                      +tax_repeal+white+citizen
                      |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$colony==1,])
  
  fe_orig13_nocov_trunc <- felm(log_trunc~prop_repeal
                          |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$colony==1,])
  
  fe_orig13_cov1_trunc <- felm(log_trunc~prop_repeal
                         +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                         |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$colony==1,])
  
  fe_orig13_cov2_trunc <- felm(log_trunc~prop_repeal
                         +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                         +tax_repeal+white+citizen
                         |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$colony==1,])
  
  dat$saver <- dat$log_div
  dat$saver_trunc <- dat$log_trunc
  
  fe_appyears_nocov <- felm(saver~prop_repeal
                          |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$indata==1,])
  
  fe_appyears_cov1 <- felm(saver~prop_repeal
                         +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                         |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$indata==1,])
  
  fe_appyears_cov2 <- felm(saver~prop_repeal
                         +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                         +tax_repeal+white+citizen
                         |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$indata==1,])
  
  fe_appyears_nocov_trunc <- felm(saver_trunc~prop_repeal
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$indata==1,])
  
  fe_appyears_cov1_trunc <- felm(saver_trunc~prop_repeal
                           +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                           |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$indata==1,])
  
  fe_appyears_cov2_trunc <- felm(saver_trunc~prop_repeal
                           +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                           +tax_repeal+white+citizen
                           |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$indata==1,])
  
  dat$saver2 <- dat$log_div
  dat$year2 <- as.numeric(as.character(dat$year))
  dat$saver2_trunc <- dat$log_trunc
  
  fe_startyear_nocov <- felm(saver2~prop_repeal
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$year2>=1802,])
  
  fe_startyear_cov1 <- felm(saver2~prop_repeal
                           +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                           |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$year2>=1802,])
  
  fe_startyear_cov2 <- felm(saver2~prop_repeal
                           +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                           +tax_repeal+white+citizen
                           |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$year2>=1802,])
  
  fe_startyear_nocov_trunc <- felm(saver2_trunc~prop_repeal
                             |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$year2>=1802,])
  
  fe_startyear_cov1_trunc <- felm(saver2_trunc~prop_repeal
                            +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$year2>=1802,])
  
  fe_startyear_cov2_trunc <- felm(saver2_trunc~prop_repeal
                            +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                            +tax_repeal+white+citizen
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat[dat$year2>=1802,])
  
  pbp_altsamp_sg <- stargazer(fe_orig13_nocov,fe_orig13_cov1,fe_orig13_cov2,fe_appyears_nocov,fe_appyears_cov1,fe_appyears_cov2,
                              fe_startyear_nocov,fe_startyear_cov1,fe_startyear_cov2,
                           add.lines = list(
                                           c("No. Clusters",length(unique(fe_orig13_nocov$clustervar[[1]])),
                                             length(unique(fe_orig13_cov1$clustervar[[1]])),
                                             length(unique(fe_orig13_cov2$clustervar[[1]])),
                                             length(unique(fe_appyears_nocov$clustervar[[1]])),
                                             length(unique(fe_appyears_cov1$clustervar[[1]])),
                                             length(unique(fe_appyears_cov2$clustervar[[1]])),
                                             length(unique(fe_startyear_nocov$clustervar[[1]])),
                                             length(unique(fe_startyear_cov1$clustervar[[1]])),
                                             length(unique(fe_startyear_cov2$clustervar[[1]])))),
                          omit.stat = c("rsq", "f", "ser","adj.rsq"),font.size = "footnotesize",
                          no.space = TRUE,column.sep.width = "-2pt",
                          dep.var.caption = c("ln(R \\& H Appropriations+1)"),
                          dep.var.labels = c("Original Colonies","Years with Any Appropriation","After First Appropriation"),
                          covariate.labels=c("No Property Requirement","Percent Enslaved",
                                             "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                             "No Tax Requirement","White Requirement","Citizen Requirement"),
                          label="rh_panel_altsamples",notes.append = FALSE,notes.label = "",
                          report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                          notes=paste("\\parbox[t]{1.05\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within states. 
                                     The unit of observation is the state-year. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                          omit.table.layout="#",
                          title="Rivers and Harbors Appropriations, Alternative Samples")
  
  pbp_altsamp_sg_trunc <- stargazer(fe_orig13_nocov_trunc,fe_orig13_cov1_trunc,fe_orig13_cov2_trunc,
                                    fe_appyears_nocov_trunc,fe_appyears_cov1_trunc,fe_appyears_cov2_trunc,
                              fe_startyear_nocov_trunc,fe_startyear_cov1_trunc,fe_startyear_cov2_trunc,
                               add.lines = list(
                                               c("No. Clusters",length(unique(fe_orig13_nocov$clustervar[[1]])),
                                                 length(unique(fe_orig13_cov1_trunc$clustervar[[1]])),
                                                 length(unique(fe_orig13_cov2_trunc$clustervar[[1]])),
                                                 length(unique(fe_appyears_nocov_trunc$clustervar[[1]])),
                                                 length(unique(fe_appyears_cov1_trunc$clustervar[[1]])),
                                                 length(unique(fe_appyears_cov2_trunc$clustervar[[1]])),
                                                 length(unique(fe_startyear_nocov_trunc$clustervar[[1]])),
                                                 length(unique(fe_startyear_cov1_trunc$clustervar[[1]])),
                                                 length(unique(fe_startyear_cov2_trunc$clustervar[[1]])))),
                              omit.stat = c("rsq", "f", "ser","adj.rsq"),font.size = "footnotesize",
                              no.space = TRUE,column.sep.width = "-2pt",
                              dep.var.caption = c("ln(R \\& H Appropriations+1)"),
                              dep.var.labels = c("Original Colonies","Years with Any Appropriation","After First Appropriation"),
                              covariate.labels=c("No Property Requirement","Percent Enslaved",
                                                 "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                                 "No Tax Requirement","White Requirement","Citizen Requirement"),
                              label="rh_panel_altsamples",notes.append = FALSE,notes.label = "",
                              report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                           notes=paste("\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within states. 
                                     The unit of observation is the state-year. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                           omit.table.layout="#",
                              title="Rivers and Harbors Appropriations, Alternative Samples")
  
  altsamps <- star_panel(pbp_altsamp_sg,pbp_altsamp_sg_trunc,panel.label.fontface="bold",
                           panel.names = c("All Appropriations", "Targeted Appropriations"), same.summary.stats = T)
  cat(altsamps, sep = '\n', file = "pbp_altsamp.tex")
  
###################################################################################################
# Coding VA and NY Differently ####################################################################
###################################################################################################
  
  dat$prop_repeal_alt1 <- dat$prop_repeal
  dat[(dat$state=="NY" & dat$year>1804 & dat$prop_repeal==0),"prop_repeal_alt1"] <- 0.5
  dat[(dat$state=="VA" & dat$year>1830 & dat$prop_repeal==0),"prop_repeal_alt1"] <- 0.5

  dat_fe_nocov_alt1 <- felm(log_div~prop_repeal_alt1
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                            ,data=dat)
  
  dat_fe_cov1_alt1   <- felm(log_div~prop_repeal_alt1
                             +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                             |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                             ,data=dat)
  
  dat_fe_cov2_alt1  <- felm(log_div~prop_repeal_alt1
                            +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                            +tax_repeal+white+citizen  
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                            ,data=dat)
  
  dat_fe_nocov_alt1_trunc <- felm(log_trunc~prop_repeal_alt1
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                            ,data=dat)
  
  dat_fe_cov1_alt1_trunc   <- felm(log_trunc~prop_repeal_alt1
                             +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                             |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                             ,data=dat)
  
  dat_fe_cov2_alt1_trunc  <- felm(log_trunc~prop_repeal_alt1
                            +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                            +tax_repeal+white+citizen  
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                            ,data=dat)
  
  dat$prop_repeal_alt1 <- dat$prop_repeal
  dat[(dat$state=="NY" & dat$year>1804 & dat$prop_repeal==0),"prop_repeal_alt1"] <- 1
  dat[(dat$state=="VA" & dat$year>1830 & dat$prop_repeal==0),"prop_repeal_alt1"] <- 1
  dat$alt_out <- dat$log_div
  dat$alt_out_trunc <- dat$log_trunc
  
  dat_fe_nocov_alt2 <- felm(alt_out~prop_repeal_alt1
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                            ,data=dat)
  
  dat_fe_cov1_alt2   <- felm(alt_out~prop_repeal_alt1
                             +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                             |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                             ,data=dat)
  
  dat_fe_cov2_alt2   <- felm(alt_out~prop_repeal_alt1
                             +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                             +tax_repeal+white+citizen  
                             |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                             ,data=dat)
  
  dat_fe_nocov_alt2_trunc <- felm(alt_out_trunc~prop_repeal_alt1
                            |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                            ,data=dat)
  
  dat_fe_cov1_alt2_trunc   <- felm(alt_out_trunc~prop_repeal_alt1
                             +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                             |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                             ,data=dat)
  
  dat_fe_cov2_alt2_trunc   <- felm(alt_out_trunc~prop_repeal_alt1
                             +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                             +tax_repeal+white+citizen  
                             |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                             ,data=dat)
  
  dat_fe_sg <- stargazer(dat_fe_nocov_alt1,dat_fe_cov1_alt1,dat_fe_cov2_alt1,
                         dat_fe_nocov_alt2,dat_fe_cov1_alt2,dat_fe_cov2_alt2,
                         notes.append = FALSE,notes.label = "",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                         omit.stat = c("rsq", "ser","f"),float.env = "table", font.size="footnotesize",
                         dep.var.caption = c("ln(R \\& H Appropriations+1)"),
                         dep.var.labels = c("Coded as `0.5'","Coded as `0'"),
                         covariate.labels=c("No Property Requirement - Alternative",
                                            
                                            "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                            "No Tax Requirement","White Requirement","Citizen Requirement"),
                         notes=paste("\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(dat_fe_nocov_alt1$clustervar[[1]])),"states. 
                                     The unit of observation is the state-year. The left three models code New York and Virginia as ``0.5'' for the periods when they have somewhat relaxed property requirements; The right three code them as ``0'', 
                        for the same years. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         label="dat_fe_alt",omit.table.layout ="#",table.placement = "H",
                         title="Rivers and Harbors Appropriations: Alternative Coding for NY and VA") 
  
  dat_fe_sg_trunc <- stargazer(dat_fe_nocov_alt1_trunc,dat_fe_cov1_alt1_trunc,dat_fe_cov2_alt1_trunc,
                         dat_fe_nocov_alt2_trunc,dat_fe_cov1_alt2_trunc,dat_fe_cov2_alt2_trunc,
                         notes.append = FALSE,notes.label = "",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                         omit.stat = c("rsq", "ser","f"),float.env = "table", font.size="footnotesize",
                         dep.var.caption = c("ln(R \\& H Appropriations+1)"),
                         dep.var.labels = c("Coded as `0.5'","Coded as `0'"),
                         covariate.labels=c("No Property Requirement - Alternative",
                                            "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                            "No Tax Requirement","White Requirement","Citizen Requirement"),
                         notes=paste("\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(dat_fe_nocov_alt1_trunc$clustervar[[1]])),"states. 
                                     The unit of observation is the state-year. The left three models code New York and Virginia as ``0.5'' for the periods when they have somewhat relaxed property requirements; The right three code them as ``0'', 
                        for the same years. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         label="dat_fe_alt",omit.table.layout ="#",
                         table.placement = "H",
                         title="Rivers and Harbors Appropriations: Alternative Coding for NY and VA") 
  
  altcoding <- star_panel(dat_fe_sg,dat_fe_sg_trunc,panel.label.fontface="bold",
                         panel.names = c("All Appropriations", "Targeted Appropriations"), same.summary.stats = T)
  cat(altcoding, sep = '\n', file = "dat_fe_alt.tex")
  
